﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RealtorsPortal.Data_Access_Layer;
using System.Data.SqlClient;
using System.Data;
using RealtorsPortal.Entities;

namespace RealtorsPortal.Business_Logic_Layer
{
    public class CategoriesDAL
    {
        DataAccessHelper daAccHelp = new DataAccessHelper();
        public DataTable GetCategoriessData()
        {
            DataTable dt = new DataTable();
            dt = daAccHelp.GetDataTable("sp_Get_Categories");
            return dt;
        }

        public DataTable GetCateByCategoryName(Category cate)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            
            param = new SqlParameter("@CategoryName", SqlDbType.VarChar);
            param.Value = cate.CategoryName;
            lstParams.Add(param);
            return daAccHelp.GetData("sp_Get_CategoryByName", lstParams);
        }

        public DataTable GetCategoryById(Category cate) {
            DataTable dt = new DataTable();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;

            param = new SqlParameter("@CategoryId", SqlDbType.Int);
            param.Value = cate.CategoryId;
            lstParams.Add(param);
            dt = daAccHelp.GetData("sp_Get_CategoryById", lstParams);
            return dt;
        }
        /// <summary>
        /// insdert category into database using procedure
        /// </summary>
        /// <param name="cate">category object to insert</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool InsertCategory(Category cate)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each catetory properties and add to params list
            ///
            ///
            ///
            param = new SqlParameter("@CategoryName",SqlDbType.VarChar);
            param.Value = cate.CategoryName;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Description", SqlDbType.VarChar);
            param.Value = cate.Description;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@IsActive", SqlDbType.Bit);
            param.Value = (cate.IsActive == true) ? 1 : 0;
            lstParams.Add(param);
            ///
            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_addCate", lstParams);
            
            //return value the indicate that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }

        /// <summary>
        /// Update category from DB method
        /// </summary>
        /// <param name="cate">new category to update</param>
        /// <param name="oldCategoryId">the ID of the category which we want to change</param>
        /// <returns>true if success, otherwise return false</returns>
        public bool UpdateCategory(Category cate)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            ///
            ///
            //get each catetory properties and add to params list
            ///
            ///
            ///
            param = new SqlParameter("@CategoryName", SqlDbType.VarChar);
            param.Value = cate.CategoryName;
            
            lstParams.Add(param);
            ///
            param = new SqlParameter("@Description", SqlDbType.VarChar);
            param.Value = cate.Description;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@IsActive", SqlDbType.Bit);
            param.Value = (cate.IsActive==true)?1:0;
            lstParams.Add(param);
            ///
            param = new SqlParameter("@OldCategoryId", SqlDbType.Int);
            param.Value = cate.CategoryId;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_updateCate", lstParams);
            
            //return value the indicate that the inserting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }


        /// <summary>
        /// Delete category from DB method
        /// </summary>
        /// <param name="CategoryId"> the cate id to delete</param>
        /// <returns>true if success, otherwise return false</returns>

        public bool DeleteCategory(int CategoryId)
        {
            List<SqlParameter> lstParams = new List<SqlParameter>();
            SqlParameter param;
            ///
            param = new SqlParameter("@CategoryId", SqlDbType.Int);
            param.Value = CategoryId;
            lstParams.Add(param);
            ///

            //Run Procedure

            int returnValue = daAccHelp.ExcuteStoredNonQuery("sp_deleteCate", lstParams);

            //return value the indicate that the Deleting was sussesed or not
            if (returnValue > 0) { return true; }
            else { return false; }
        }
    }

}